package cn.tedu.aiumall.user.webapi.controller;


import cn.tedu.aiumall.common.pojo.user.dto.UserAddNewDTO;
import cn.tedu.aiumall.common.pojo.user.dto.UserLoginDTO;
import cn.tedu.aiumall.user.service.IUserService;
import cn.tedu.aiumall.user.webapi.sendSms.ISendMessageService;
import cn.tedu.aiumall.user.webapi.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/users")
@Api(tags = "用户管理模块")
public class UserController {
    @Autowired
    private IUserService service;

    @Autowired
    ISendMessageService sendMessageService;

    public UserController() {
        log.debug("创建控制器userController");
    }

    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 100)
//    @PreAuthorize("user/read")
    @PostMapping("/reg")
    public JsonResult<Void> reg(UserAddNewDTO userAddNewDTO){
        log.debug("开始处理注册用户的请求,参数{}",userAddNewDTO);
        service.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 88 )
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("开始处理【登录认证】的请求，参数：{}", userLoginDTO);
        String jwt = service.login(userLoginDTO);
        log.debug("即将向客户端响应JWT数据：{}", jwt);
        return JsonResult.ok(jwt);
    }
    @PostMapping("/sendMessage")
    @ApiOperation("发送登录验证码")
    @ApiOperationSupport(order = 80)
    public JsonResult<Void> sendMessage(UserLoginDTO userLoginDTO){
        log.debug("开始处理【发送手机验证码登录】的请求：参数：{}",userLoginDTO);
        String sendMessage=sendMessageService.sendMessage(userLoginDTO);
        log.debug("即将向客户端响应SendMessage数据：{}", sendMessage);
        return JsonResult.ok();
    }

    @PostMapping("/addNewMessage")
    @ApiOperation("发送注册验证码")
    @ApiOperationSupport(order = 81)
    public JsonResult<Void> addNewMessage(UserAddNewDTO userAddNewDTO){
        log.debug("开始处理【发送手机验证码注册】的请求：参数：{}",userAddNewDTO);
        String sendMessage=sendMessageService.addNewMessage(userAddNewDTO);
        log.debug("即将向客户端响应SendMessage数据：{}", sendMessage);
        return JsonResult.ok();
    }
}
